﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 索引
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1[1] = "张三";
            Console.WriteLine(p1[1] + p1[2]);
            Console.WriteLine(p1["tom",3,9]);
        }
    }

    class Person
    {
        public string FirstName = "大明";
        public string SecondName = "小明";

        public string this[int index]  //string 索引器的数据类型 this---相当于索引器的一个特殊的函数名，替代本对向的名称
        {
            set
            {
                if (1 == index)
                {
                    FirstName = value;
                }
                else if (2 == index)
                {
                    SecondName = value;
                }
                else
                {
                    throw new Exception("错误的序号");
                }
            }
            get
            {
                if (1 == index)
                {
                    return FirstName;
                }
                else if (2 == index)
                {
                    return SecondName;
                }
                else
                {
                    throw new Exception("错误的序号");
                }
            }
        }


        //索引也可以重载，这个例子没什么实际意义，只是为了说明索引的参数不一定为int型而已
        public string this[string name, int x, int y]
        {
            get
            {
                return name + x + y;
            }
        }

    }
}
